home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / exec.arc / EXECF.C < prev    next >
Text File  |  1985-05-28  |  4KB  |  161 lines

  1. /*        execf.c        execute another program and return status */
  2.  
  3. #include <stdio.h>
  4.  
  5. struct {unsigned int ax,bx,cx,dx,si,di,ds,es;} r;
  6. unsigned char cline[128],*ep,*envfind();
  7. int status=-1;
  8.  
  9. #define NUMINTS 30
  10. static char *internals[] = {"copy","ren","rename","del","erase","chdir","cd",\
  11. "md","mkdir","dir","rd","rmdir","type","cls","ctty","date","time",\
  12. "path","prompt","set","ver","verify","vol","echo","rem","goto","if",\
  13. "shift","pause","for"};
  14.  
  15. fork(cmdl)                /* parses off name and cmdl for execf */
  16. char *cmdl;
  17. {
  18.     char *sp;
  19.     char *name;
  20.     char comlin[128];
  21.  
  22.     sp = strcpy(name,cmdl);
  23.     strcat(name," ");                    /* pad for find */
  24.     while(*++sp != ' ');                /* find 1st space */
  25.     *sp++ = '\0';                        /* term name str */
  26.     strcpy(comlin,sp);                /* get command line */
  27.     return(execf(name,comlin));    /* launch it */
  28. }
  29.  
  30. execf(name,cmdl)        /* executes name with command line cmdl */
  31. char *name;
  32. char *cmdl;
  33. {
  34.     int i;
  35.     char tp[255];
  36.     char *ts;
  37.     char *path;
  38.     char *findexecf();
  39.     char lcmdl[255];        /* local command line */
  40.     
  41.     lcmdl[0] = ' ';            /* tack on a space at the beginning */
  42.     strcpy(lcmdl+1,cmdl);    /* to keep DOS happy */
  43.     name = lower(name);        /* convert to lowercase for search */
  44.     for (i=0;i<NUMINTS;i++) {
  45.         if (!(strcmp(name,internals[i])))    /* check for an internal command */
  46.             return (system(strcat(name,lcmdl)));    /* use c86 system() if internal */
  47.     }
  48.     /* it's not an internal command, process it */
  49.     
  50.     path = alloc(255);
  51.     curdrv(path);            /* get current drive */
  52.     getpath(path);        /* and path */
  53.     ep = envfind("PATH");
  54.     if (*ep != ';') strcat(path,";");    /* if none at beginning already */
  55.     strcat(path,ep);                            /* all one path string */
  56.     free(ep);
  57.     while (*path) {                            /* until we hit EOS */
  58.         strcpy(tp,path);
  59.         i = 0;
  60.         while (*path++ != ';') i++;        /* find end of path */
  61.         tp[i] = '\0';                            /* term path string */
  62.         if (tp[i-2] != '\\' || tp[i-2] != '/') strcat(tp,"/"); 
  63.         if ((ts=findexecf(tp,name))) {
  64.             return(_execf(ts,lcmdl));
  65.         }
  66.     }
  67.     puts("\nexecf: command not found");
  68.     return(-1);                                /* command not found, ret err */
  69. }
  70.  
  71. char *findexecf(path,name)                /* tries to find an execable name along path */
  72. char *path,*name;
  73. {
  74.     char tn[255];
  75.     char *first,*next;
  76.     static char *exet[] = {".COM",".EXE",".BAT"};
  77.     int i;
  78.  
  79.     name = upper(name);
  80.     strcpy(tn,path);
  81.     strcat(tn,name);
  82.     strcat(tn,".*");            /* add wildcard to find all */
  83.     first = filedir(tn,0);    /* look for all normal files */
  84.     strcpy(tn,name);
  85.     for (next=first;*next != '\0';) {
  86.         for (i=0;i<3;i++) {
  87.             if (!(strcmp(strcat(tn,exet[i]),next))) return(strcat(path,tn));
  88.             strcpy(tn,name);        /* nope, try again */
  89.         }
  90.         next += strlen(next)+1;        /* look at next name in list */
  91.     }
  92.     return(NULL);                        /* didn't find one */
  93. }
  94.  
  95. _execf(path,cmdl)                /* execs path(incl. name.ext) with cmdl */
  96. char *path,*cmdl;
  97. {
  98.     struct {
  99.         int env_seg;
  100.         char *line_off,*line_seg;
  101.         char *fcb1_off,*fcb1_seg;
  102.         char *fcb2_off,*fcb2_seg;
  103.     } ctrl;
  104.     
  105.     cline[0] = strlen(cmdl);        /* put in length */
  106.     strcpy(cline+1,cmdl);            /* copy in command line */
  107.     strcat(cline,"\r");                /* add a CR */
  108.     segread(&r.si);                    /* get current cs */
  109.     ctrl.env_seg=0;                    /* no environment to pass */
  110.     ctrl.line_off=cline;                /* offset of command line */
  111.     ctrl.line_seg=r.ds;                /* segment of command line */
  112.     return(loadexec(path,r.ds,&ctrl,r.ds,0));    /* launch! */
  113. }
  114.  
  115. /* curdrv - get current default drive */
  116.  
  117. curdrv(sp)
  118. char *sp;
  119. {
  120.     struct { int ax, bx, cx;
  121.              char *dx, *si, *di, *ds, *es;
  122.            } r;
  123.  
  124.     r.ax = 0x1900;                            /* DOS interrupt 19 */
  125.     sysint(0x21, &r, &r);                /* gets current drive number */
  126.     *sp++ = r.ax + 'a';                    /* convert to symbolic drive name */
  127.     *sp++ = ':';
  128.     *sp++ = '\0';
  129.     sp--;
  130.     return;
  131. }
  132.  
  133. /* getpath - get path to directory on indicated drive */
  134.  
  135. getpath(sp)
  136. char *sp;
  137. {
  138.     struct { int ax, bx, cx, dx;
  139.              char *si;
  140.              int di, ds, es;
  141.            } r;
  142.     extern int _showds();
  143.  
  144.     strcat(sp, "/");                /* append root file symbol to drive name */
  145.     
  146.     r.ax = 0x4700;                    /* DOS interrupt 47 gets path string */
  147.     r.dx = *sp - '`';                /* convert drive name to index */
  148.     r.ds = _showds();
  149.     r.si = sp + 3;                    /* paste string after root symbol */
  150.     sysint(0x21, &r, &r);
  151.     return;
  152. }
  153.  
  154. _showds()
  155. {
  156.     struct{int cs, ss, ds, es;} r;
  157.  
  158.     segread(&r);
  159.     return(r.ds);
  160. }
  161.